﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xwork.SEO.WebSite.Models;
namespace Xwork.SEO.WebSite.Controllers
{
    [UserAuthorize]
    public class MemberController : SEOController
    { 

        public ActionResult Index()
        {
            User user = db.Users.Include("UserInfo").First(p => p.ID == User.ID);
            this.User = user;
            UserInfoModel model = new UserInfoModel();

            if (user.UserInfo != null)
            {
                model.Address = user.UserInfo.Address;
                model.Area = user.UserInfo.Area;
                model.Company = user.UserInfo.Company;
                model.Fax = user.UserInfo.Fax;
                model.Linkman = user.UserInfo.Linkman;
                model.Mobile = user.UserInfo.Mobile;
                model.Phone = user.UserInfo.Phone;
                model.UserEmail = user.UserInfo.UserEmail;
                model.Pic = user.UserInfo.Licence;
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Index(UserInfoModel model)
        {
            if (ModelState.IsValid)
            {
                HttpPostedFileBase file = Request.Files["Pic1"];
                if (file == null || string.IsNullOrEmpty(file.FileName))
                {
                    this.ModelState.AddModelError("Pic1", "请选择营业执照");
                }
                else
                {
                    string ext = System.IO.Path.GetExtension(file.FileName);
                    if (!new string[] { ".jpg", ".gif", ".png", ".jpeg" }.Any(p => p.Equals(ext, StringComparison.OrdinalIgnoreCase)))
                    {
                        this.ModelState.AddModelError("Pic", "文件格式不正确");
                    }
                    else
                    {
                        string dir = Server.MapPath("~/upfiles/");
                        model.Pic = "/upfiles/" + Guid.NewGuid().ToString() + ext;
                        if (!System.IO.Directory.Exists(dir))
                        {
                            System.IO.Directory.CreateDirectory(dir);
                        }
                        file.SaveAs(Server.MapPath("~" + model.Pic));

                        User user = db.Users.Include("UserInfo").First(p => p.ID == User.ID);

                        if (user.UserInfo == null)
                        {
                            user.UserInfo = new UserInfo();
                        }

                        user.UserInfo.ID = user.ID;
                        user.UserInfo.Address = model.Address;
                        user.UserInfo.Area = model.Area;
                        user.UserInfo.Company = model.Company;
                        user.UserInfo.Fax = model.Fax;
                        user.UserInfo.Linkman = model.Linkman;
                        user.UserInfo.Mobile = model.Mobile;
                        user.UserInfo.Phone = model.Phone;
                        user.UserInfo.UserEmail = model.UserEmail;
                        user.UserInfo.Licence = model.Pic;
                        db.SaveChanges();
                        return View(model);
                    }
                }
            }
            return View(model);
        }



        [ChildActionOnly]
        public ActionResult UserInfoLeft()
        {
            User user = db.Users.Find(User.ID);
            //int sites = user.SeoSites.Count;
            //int keywords = user.SeoSites.Sum(p => p.SeoKeywords.Count);
            return PartialView(user);
        }


        
       



        #region 会员中心

        //
        // GET: /Member/

        public ActionResult ChangePassword()
        {

            return View();
        }

        [HttpPost]
        public ActionResult ChangePassword(ChangePasswordModel model)
        {
            User user = (User)Session["user"];
            db.ChangePassword(user.ID, model.OldPassword, model.NewPassword);
            return Json(new { message = "修改成功", url = Url.Action("ChangePassword") });
        }
        
        public ActionResult CheckOldPassword(string oldPassword)
        {
            User user = (User)Session["user"];
            return Json(db.CheckPassword(user.ID, oldPassword), JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 用户信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Info()
        {
            User user = db.Users.Include("UserInfo").FirstOrDefault(p => p.ID == User.ID);
            return View(user);
        }


        [HttpPost]
        public ActionResult EditInfo(UserInfo model)
        {
            //TODO 添加保存用户信息逻辑
            return View();
        }
        #endregion



    }
}
